package org.gap.service;

import java.util.List;
import java.util.Set;

import org.gap.dao.PatientDAO;
import org.gap.dao.RendezVousDAO;
import org.gap.dao.TiersDAO;

import org.gap.domain.Patient;
import org.gap.domain.RendezVous;
import org.gap.domain.Tiers;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Patient entities
 * 
 */

@Service("PatientService")
@Transactional
public class PatientServiceImpl implements PatientService {

	/**
	 * DAO injected by Spring that manages Patient entities
	 * 
	 */
	@Autowired
	private PatientDAO patientDAO;

	/**
	 * DAO injected by Spring that manages RendezVous entities
	 * 
	 */
	@Autowired
	private RendezVousDAO rendezVousDAO;

	/**
	 * DAO injected by Spring that manages Tiers entities
	 * 
	 */
	@Autowired
	private TiersDAO tiersDAO;

	/**
	 * Instantiates a new PatientServiceImpl.
	 *
	 */
	public PatientServiceImpl() {
	}

	/**
	 * Delete an existing Patient entity
	 * 
	 */
	@Transactional
	public void deletePatient(Patient patient) {
		patientDAO.remove(patient);
		patientDAO.flush();
	}

	/**
	 * Load an existing Patient entity
	 * 
	 */
	@Transactional
	public Set<Patient> loadPatients() {
		return patientDAO.findAllPatients();
	}

	/**
	 * Save an existing Patient entity
	 * 
	 */
	@Transactional
	public void savePatient(Patient patient) {
		Patient existingPatient = patientDAO.findPatientByPrimaryKey(patient.getPatPatient());

		if (existingPatient != null) {
			if (existingPatient != patient) {
				existingPatient.setPatPatient(patient.getPatPatient());
				existingPatient.setPatDateEntree(patient.getPatDateEntree());
				existingPatient.setPatTelContact(patient.getPatTelContact());
				existingPatient.setPatDivers(patient.getPatDivers());
				existingPatient.setPatDateSortie(patient.getPatDateSortie());
				existingPatient.setPatAxeMedical(patient.getPatAxeMedical());
				existingPatient.setPatSousAxeMedical(patient.getPatSousAxeMedical());
				existingPatient.setPatMotifSortie(patient.getPatMotifSortie());
				existingPatient.setPatDiagnostic(patient.getPatDiagnostic());
				existingPatient.setPatDateDerniereVisite(patient.getPatDateDerniereVisite());
			}
			patient = patientDAO.store(existingPatient);
		} else {
			patient = patientDAO.store(patient);
		}
		patientDAO.flush();
	}

	/**
	 * Save an existing RendezVous entity
	 * 
	 */
	@Transactional
	public Patient savePatientRendezVouses(Integer patPatient, RendezVous related_rendezvouses) {
		Patient patient = patientDAO.findPatientByPrimaryKey(patPatient, -1, -1);
		RendezVous existingrendezVouses = rendezVousDAO.findRendezVousByPrimaryKey(related_rendezvouses.getRdvRendezVous());

		// copy into the existing record to preserve existing relationships
		if (existingrendezVouses != null) {
			existingrendezVouses.setRdvRendezVous(related_rendezvouses.getRdvRendezVous());
			existingrendezVouses.setRdvTiers(related_rendezvouses.getRdvTiers());
			existingrendezVouses.setRdvSpecialite(related_rendezvouses.getRdvSpecialite());
			existingrendezVouses.setRdvDate(related_rendezvouses.getRdvDate());
			existingrendezVouses.setRdvAdmission(related_rendezvouses.getRdvAdmission());
			related_rendezvouses = existingrendezVouses;
		} else {
			related_rendezvouses = rendezVousDAO.store(related_rendezvouses);
			rendezVousDAO.flush();
		}

		related_rendezvouses.setPatient(patient);
		patient.getRendezVouses().add(related_rendezvouses);
		related_rendezvouses = rendezVousDAO.store(related_rendezvouses);
		rendezVousDAO.flush();

		patient = patientDAO.store(patient);
		patientDAO.flush();

		return patient;
	}

	/**
	 * Return all Patient entity
	 * 
	 */
	@Transactional
	public List<Patient> findAllPatients(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Patient>(patientDAO.findAllPatients(startResult, maxRows));
	}

	/**
	 * Delete an existing Tiers entity
	 * 
	 */
	@Transactional
	public Patient deletePatientTiers(Integer patient_patPatient, Integer related_tiers_tieTiers) {
		Patient patient = patientDAO.findPatientByPrimaryKey(patient_patPatient, -1, -1);
		Tiers related_tiers = tiersDAO.findTiersByPrimaryKey(related_tiers_tieTiers, -1, -1);

		patient.setTiers(null);
		related_tiers.getPatients().remove(patient);
		patient = patientDAO.store(patient);
		patientDAO.flush();

		related_tiers = tiersDAO.store(related_tiers);
		tiersDAO.flush();

		tiersDAO.remove(related_tiers);
		tiersDAO.flush();

		return patient;
	}

	/**
	 * Save an existing Tiers entity
	 * 
	 */
	@Transactional
	public Patient savePatientTiers(Integer patPatient, Tiers related_tiers) {
		Patient patient = patientDAO.findPatientByPrimaryKey(patPatient, -1, -1);
		Tiers existingtiers = tiersDAO.findTiersByPrimaryKey(related_tiers.getTieTiers());

		// copy into the existing record to preserve existing relationships
		if (existingtiers != null) {
			existingtiers.setTieTiers(related_tiers.getTieTiers());
			existingtiers.setTieIdentClient(related_tiers.getTieIdentClient());
			existingtiers.setTieTypeTiers(related_tiers.getTieTypeTiers());
			existingtiers.setTieLibExterne(related_tiers.getTieLibExterne());
			existingtiers.setTieNationalite(related_tiers.getTieNationalite());
			existingtiers.setTieTelFix(related_tiers.getTieTelFix());
			existingtiers.setTieTelBureau(related_tiers.getTieTelBureau());
			existingtiers.setTieTelPortable(related_tiers.getTieTelPortable());
			existingtiers.setTieFax(related_tiers.getTieFax());
			existingtiers.setTieEMail(related_tiers.getTieEMail());
			existingtiers.setTieDateCreation(related_tiers.getTieDateCreation());
			existingtiers.setTieUserCreation(related_tiers.getTieUserCreation());
			related_tiers = existingtiers;
		}

		patient.setTiers(related_tiers);
		related_tiers.getPatients().add(patient);
		patient = patientDAO.store(patient);
		patientDAO.flush();

		related_tiers = tiersDAO.store(related_tiers);
		tiersDAO.flush();

		return patient;
	}

	/**
	 */
	@Transactional
	public Patient findPatientByPrimaryKey(Integer patPatient) {
		return patientDAO.findPatientByPrimaryKey(patPatient);
	}

	/**
	 * Delete an existing RendezVous entity
	 * 
	 */
	@Transactional
	public Patient deletePatientRendezVouses(Integer patient_patPatient, Integer related_rendezvouses_rdvRendezVous) {
		RendezVous related_rendezvouses = rendezVousDAO.findRendezVousByPrimaryKey(related_rendezvouses_rdvRendezVous, -1, -1);

		Patient patient = patientDAO.findPatientByPrimaryKey(patient_patPatient, -1, -1);

		related_rendezvouses.setPatient(null);
		patient.getRendezVouses().remove(related_rendezvouses);

		rendezVousDAO.remove(related_rendezvouses);
		rendezVousDAO.flush();

		return patient;
	}

	/**
	 * Return a count of all Patient entity
	 * 
	 */
	@Transactional
	public Integer countPatients() {
		return ((Long) patientDAO.createQuerySingleResult("select count(o) from Patient o").getSingleResult()).intValue();
	}
}
